﻿using System;
using System.Collections.Generic;

namespace ExpressionEvalSample.Domain
{
    public enum AirplaneType
    {
        Jet       = 0,
        Propeller = 1,
        Glider    = 2,
        Kite      = 3
    }

    public class Model
    {
        private static readonly Random _random = new Random();

        public Model()
        {
            Date = DateTime.Now;
        }

        public void PutPlaneInAir(Airplane plane)
        {
            if (PlanesOnTheGround.Contains(plane))
                PlanesOnTheGround.Remove(plane);
            
            PlanesInTheAir.Add(plane);
            plane.CurrentSpeed = _random.NextDouble() * plane.MaxSpeed;
            plane.Altitude = _random.Next(30000);

        }

        public void PutPlaneOnGround(Airplane plane)
        {
            if (PlanesInTheAir.Contains(plane))
                PlanesInTheAir.Remove(plane);
            PlanesOnTheGround.Add(plane);
            plane.CurrentSpeed = 0;
            plane.Altitude = 0;

        }

        public List<Airplane> PlanesInTheAir { get; set; }
        public List<Airplane> PlanesOnTheGround { get; set; }

        public DateTime Date { get; set; }

        public void GenerateTestData(int numPlanesInTheAir,int numPlanesOnTheGround)
        {
            PlanesInTheAir = new List<Airplane>();
            PlanesOnTheGround = new List<Airplane>();
            
            for (int i=0;i<numPlanesInTheAir;i++)
            {
                PutPlaneInAir(CreateRandomPlane());
            }

            for (int i=0;i<numPlanesOnTheGround;i++)
            {
                PutPlaneOnGround(CreateRandomPlane());
            }
        }

        private static Airplane CreateRandomPlane()
        {

            var newPlane = new Airplane
            {
                TailNumber = "N" + _random.Next(500),
                MaxSpeed = _random.Next(500),
                Type = (AirplaneType)_random.Next(0, 4),
                Pilot = new Person
                {
                    Name = "Pilot " + _random.Next(1000),
                    DateOfBirth =
                        DateTime.Today.AddYears(-1 * _random.Next(20, 61))
                }

            };
            newPlane.Passengers = new List<Passenger>();
            int count = _random.Next(0, 5);
            for (int p = 0; p < count; p++)
            {
                newPlane.Passengers.Add(
                    new Passenger
                    {
                        SeatAssignment = "Seat " + _random.Next(1, count),
                        Name = "Passenger " + (_random.Next(500)),
                        DateOfBirth = DateTime.Today.AddYears(-1 * _random.Next(0, 100))
                    }
                    );
            }

            return newPlane;
        }
    }

    public class Airplane
    {
        public string TailNumber { get; set; }
        public int MaxSpeed { get; set; }
        public double CurrentSpeed { get; set; }
        public AirplaneType Type { get; set; }
        public int Altitude { get; set; }

        public Person Pilot { get; set; }

        public List<Passenger> Passengers;
    }

    public class Passenger : Person
    {
        public string SeatAssignment;
    }

    public class Person
    {
        public string Name { get; set; }
        public DateTime DateOfBirth { get; set; }
        public double AgeInYears { get { return DateTime.Today.Subtract(DateOfBirth).Days / 365.0; } }
    }
}
